﻿#region Namespace References

using System;
using DynConDevCorp.OpenArch.SmartData.Core;
using DynConDevCorp.OpenArch.SmartData.Interfaces;
using DynConDevCorp.OpenArch.SmartData.Interfaces.Attributes;
using DynConDevCorp.OpenArch.SmartData.Interfaces.EventArgClasses;

#endregion

namespace DynConDevCorp.OpenArch.SmartData.BillingDemo1.ApplicationBusinessObjects
{
    public class Order : SmartEntity
    {
        #region Constructors

        public Order()
        {
        }

        public Order(Guid EntityID)
            : base(EntityID)
        {
        }

        #endregion

        #region Public Instance Properties

        [AutoField]
        public ISmartEntityLink<Customer> Customer { get; private set; }

        [AutoField]
        [Persist(IsXmlPersistable = false, IsDatabaseField = true, DatabaseFieldName = "CustomerID")]
        public ISmartField<String> CustomerCode { get; private set; }

        [AutoField]
        public ISmartField<String> EmployeeID { get; private set; }

        [AutoField]
        public ISmartField<String> Freight { get; private set; }

        [AutoField]
        public SmartEntityCollection<OrderDetail> Items { get; private set; }

        [AutoField(InputInhibited = true, DisplayFormatString = "C2", ValueChanged = "Recalculate_Tax")]
        public ISmartField<decimal> ItemsTotalPrice { get; private set; }

        [AutoField(InputInhibited = true, DisplayFormatString = "C2", ValueChanged = "Recalculate_Total")]
        public ISmartField<decimal> ItemsTotalTax { get; private set; }

        [AutoField(InputInhibited = true, ValueChanged = "Recalculate_Shipping")]
        public ISmartField<decimal> ItemsTotalWeight { get; private set; }

        [AutoField]
        public ISmartField<String> OrderDate { get; private set; }

        [AutoField]
        [Persist(IsXmlPersistable = true, IsDatabaseField = true, DatabaseFieldName = "OrderID")]
        public ISmartField<String> OrderNumber { get; private set; }

        [AutoField(InputInhibited = true, DisplayFormatString = "C2")]
        public ISmartField<decimal> OrderTotal { get; private set; }

        [AutoField]
        public ISmartField<String> RequiredDate { get; private set; }

        [AutoField]
        public ISmartField<String> ShipAddress { get; private set; }

        [AutoField]
        public ISmartField<String> ShipCity { get; private set; }

        [AutoField]
        public ISmartField<String> ShipCountry { get; private set; }

        [AutoField]
        public ISmartField<String> ShipName { get; private set; }

        [AutoField]
        public ISmartField<String> ShippedDate { get; private set; }

        [AutoField(InputInhibited = true, DisplayFormatString = "C2", ValueChanged = "Recalculate_Total")]
        public ISmartField<decimal> Shipping { get; private set; }

        [AutoField(ValueChanged = "Recalculate_Shipping")]
        public ISmartField<decimal> ShippingRate { get; private set; }

        [AutoField]
        public ISmartField<String> ShipPostalCode { get; private set; }

        [AutoField]
        public ISmartField<String> ShipRegion { get; private set; }

        [AutoField]
        public ISmartField<String> ShipVia { get; private set; }

        [AutoField(ValueChanged = "Recalculate_Tax")]
        public ISmartField<decimal> TaxRate { get; private set; }

        #endregion

        #region Private Instance Methods

        private void Recalculate_Shipping(object sender, ValueChangedEventArgs<decimal> e)
        {
            if ((ItemsTotalWeight == null) || (ShippingRate == null))
                return;
            Shipping.Value = ItemsTotalWeight.Value * ShippingRate.Value;
        }

        private void Recalculate_Tax(object sender, ValueChangedEventArgs<decimal> e)
        {
            if ((ItemsTotalPrice == null) || (TaxRate == null))
                return;
            ItemsTotalTax.Value = ItemsTotalPrice.Value*TaxRate.Value;
        }

        private void Recalculate_Total(object sender, ValueChangedEventArgs<decimal> e)
        {
            if ((ItemsTotalPrice == null) || (ItemsTotalTax == null) || (Shipping == null))
                return;
            OrderTotal.Value = ItemsTotalPrice.Value + ItemsTotalTax.Value + Shipping.Value;
        }

        #endregion
    }
}